# !/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import urllib2
import json
import time
from mns_demo import ali_mns
from simlog import Logger
from sql_tmp import MysqlElephantV2, MysqlCompareV2

enqueue_log = Logger('./log/yyx.log', logging.WARNING, logging.DEBUG)

# 	 	$runTimes 定义说明
# 	    $runTimes = 1 时为已经入队（初始化入队0改1）
# 	    $runTimes = 2 出队状态
# 	    $runTimes = 3 符合surf值要求的状态
# 	    $runTimes = 10 时为高宽比不符合要求的数据
# 	    $runTimes = 11 获取到result_id 和score 失败的数据
# 	    $runTimes = 13 surf不符合要求的数据状态
# 	    $runTimes = 14 通过result_id接口获取不到的数据的状态
# 	    $runTimes = 15 通过result_id查出破损或者纯色图状态
# 	    $runTimes = 16 通过关键词过滤掉的数据状态
# 	 	$runTimes > 10 的数据都是有问题的数据


###################################  elepnant_v2   #####################################


obj_ele = MysqlElephantV2()


# 获取一条status为4的task
def get_ele_task_tastus(tabname, status):
	sql = "SELECT id,company_id,assign_spider from %s WHERE `status`=%s limit 1" % (tabname, status)
	print(sql)
	data = obj_ele.find(sql, 2)
	print(data)
	task_id = data[0]
	company_id = data[1]
	table_name = 'compare' + '_' + str(company_id)
	assign_spider = data[2]
	
	return task_id, table_name, assign_spider


def updata_ele_task_status(tabname,status, task_id):
	sql = "update %s set status=%s  where id=%s" % (tabname, status, task_id)
	# print sql
	obj_ele.cud(sql)


###################################  compare_v2   #####################################

obj_com = MysqlCompareV2()


# 查询没有对比过的
def compare_v2(tabname, task_id):
	sql = "select id,company_id,task_id,run_times,oss_url,width,height,size,client_date,url,clue_id,pic_url,is_cover " \
	      "from %s " \
	      "WHERE task_id=%s AND img_format<3 AND run_times=0   limit 2000" % (tabname, task_id)
	
	# print(sql)
	data = obj_com.find(sql, 1)
	print data
	return data


def get_compare_v2_task_count(tabname, task_id, run_times):
	sql = """SELECT COUNT(*) FROM %s WHERE task_id=%s AND run_times=%s""" % (tabname, task_id, run_times)
	
	# print sql
	num = obj_com.find(sql, 2)
	print num
	return num


# 修改run_times
def update_run_times(tabname, run_times_status, id):
	sql = "update %s set run_times =%s where id =%s" % (tabname, run_times_status, id)
	# print sql
	obj_com.cud(sql)


def create_msg_body():
	pass


def main():
	task_id, tabname, assign_spider = get_ele_task_tastus('ele_task', 40)
	# print task_id, tabname
	# print type(tabname)
	
	updata_ele_task_status('ele_task',50, task_id)
	
	compare_v2_data = compare_v2(tabname, task_id)
	n = 0
	for d_line in compare_v2_data:

		n += 1
		print n
		compare_id = d_line[0]
		company_id = d_line[1]
		task_id = d_line[2]
		run_times = d_line[3]
		oss_url = d_line[4]
		width = d_line[5]
		height = d_line[6]
		size = d_line[7]
		client_date = d_line[8]
		url = d_line[9]
		clue_id = d_line[10]
		pic_url = d_line[11]
		is_cover = d_line[12]
		
		#过滤长宽比不符合的条件
		if width * height < 2205 or width / height < 0.3062 or width / height > 3.26 or size == 0:
			update_run_times(tabname, 10, compare_id)
			continue
		
		msg_body = {}
		msg_body["compare_id"] = compare_id
		msg_body["company_id"] = company_id
		msg_body["task_id"] = task_id
		msg_body["img_url"] = pic_url
		msg_body["assign_spider"] = assign_spider
		msg_body["compare_type"] = 0
		msg_body["client_date"] = client_date
		msg_body["clue_id"] = clue_id
		msg_body["url"] = url
		msg_body["oss_url"] = oss_url
		msg_body["is_cover"] = is_cover
		if assign_spider == 2:
			msg_body["surf_url"] = oss_url
		else:
			msg_body[
				"surf_url"] = "http://elephant-spider.vpc100-oss-cn-beijing.aliyuncs.com/" + oss_url + "?x-oss-process=image/resize,m_lfit,h_400,w_400"
		
		# print  msg_body
		msg_body_json = json.dumps(msg_body)
		# print msg_body_json
		a = ali_mns('elephant-test-one', "elephant-test-two")
		try:
			ss = a.send_data(msg_body_json)
			update_run_times(tabname, 1, compare_id)
		except:
			continue
		
	num = get_compare_v2_task_count(tabname, task_id, 0)[0]
	print num
	if num < 10:
		updata_ele_task_status('ele_task',70, task_id)
		
	else:
		updata_ele_task_status('ele_task',40, task_id)
	


if __name__ == '__main__':
	while 1:
		main()
